package com.jk.mindskip.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jk.mindskip.dao.TbAdminMapper;
import com.jk.mindskip.dto.SysMenu;
import com.jk.mindskip.dto.TbAdmin;
import com.jk.mindskip.pojo.PagedResponse;
import com.jk.mindskip.service.TbAdminService;
import com.jk.mindskip.utils.PaginationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class TbAdminServiceImpl implements TbAdminService {

    @Autowired
    private TbAdminMapper adminMapper;

    @Override
    public PagedResponse<TbAdmin> findAdminList(TbAdmin admin, Integer pageNum, Integer pageSize) {
        Page<TbAdmin> page = new Page<>(pageNum, pageSize);
        QueryWrapper<TbAdmin> queryWrapper = new QueryWrapper<>();
        Page<TbAdmin> adminPage = adminMapper.selectPage(page, queryWrapper);
        return PaginationUtil.getPaginationResponse(adminPage.getRecords(), pageNum, pageSize, adminPage.getTotal());
    }

    @Override
    public void addAdmin(TbAdmin admin) {
        adminMapper.insert(admin);
    }

    @Override
    public TbAdmin findTbAdminByUserName(String username) {
        QueryWrapper<TbAdmin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name", username);
        TbAdmin tbAdmin = adminMapper.selectOne(queryWrapper);
        if(Objects.isNull(tbAdmin)){
            throw new RuntimeException("用户不存在");
        }

        List<SysMenu> menus = new ArrayList<>();

        //如果当前登录人存在,通过当前用户id获得当前的登录人的菜单集合
        List<SysMenu> menuList = adminMapper.findMenuListByUserId(tbAdmin,0L);
        menuList.stream().forEach(item -> {
            //判断当前节点下是否有子节点
            if (isExsistsChildrenNodes(tbAdmin, item)) {
                //查询该节点的字节信息
                List<SysMenu> menuChildrenNodes = findMenuChildrenNodes(tbAdmin, item);
                item.setChildren(menuChildrenNodes);
            }
            menus.add(item);
        });

        tbAdmin.setMenus(menus);
        return tbAdmin;
    }

    /**
     * @description:查询当前节点下的所有子节点，递归操作
     * @author: wsy 
     * @date: 2024/12/11 下午3:29
     * @param: [tbAdmin, item]
     * @return: java.util.List<com.jk.mindskip.dto.SysMenu>
     **/
    private List<SysMenu> findMenuChildrenNodes(TbAdmin admin, SysMenu sysMenu) {
        List<SysMenu> menus = new ArrayList<>();
        List<SysMenu> menuListByUserId = adminMapper.findMenuListByUserId(admin, sysMenu.getMenuId());
        menuListByUserId.stream().forEach(item -> {
            //判断当前节点下是否有子节点
            if (isExsistsChildrenNodes(admin, item)) {
                //查询该节点的字节信息
                List<SysMenu> menuChildrenNodes = findMenuChildrenNodes(admin, item);
                item.setChildren(menuChildrenNodes);
            }
            menus.add(item);
        });
        return menus;
    }

    /**
     * @description:判断当前节点是否有子节点
     * @author: wsy 
     * @date: 2024/12/11 下午3:23
     * @param: [tbAdmin, item]
     * @return: boolean
     **/
    private boolean isExsistsChildrenNodes(TbAdmin admin, SysMenu item) {
        boolean flag = true;
        List<SysMenu> menuListByUserId = adminMapper.findMenuListByUserId(admin, item.getMenuId());
        if (CollectionUtils.isEmpty(menuListByUserId)) {
            flag = false;
        }
        return flag;
    }

    @Override
    public List<String> selectPermsByUserId(Integer id) {
        return adminMapper.selectPermsByUserId(id);
    }
}
